<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of NodeModel
 *
 * @author John
 */
namespace Admin\Model;
use Think\Model\RelationModel;

class RoleModel extends RelationModel {
    protected $_auto = array(
        
    );
    
    protected $_link = array(
        'User' => array(
            'mapping_type'  => self::MANY_TO_MANY,
            'class_name'            => 'User',
            'foreign_key'           => 'role_id',
            'relation_foreign_key'  => 'user_id',
            'relation_table'        => 'zh_role_user',
        ),
        'Node' => array(
            'mapping_type'  => self::MANY_TO_MANY,
            'class_name'            => 'Node',
            'foreign_key'           => 'role_id',
            'relation_foreign_key'  => 'node_id',
            'relation_table'        => 'zh_access',
        ),
    );
    
    public function deleteRole($ids){
        $ids = explode(",", $ids);
        foreach ($ids as $id) {
            $user_count = D("RoleUser")->where(array("role_id" => $id))->count();
            if($user_count)
            {
                //用户存在，无法删除角色
                return 10001;
            }
            $ret = $this->where(array("id" => $id))->delete();
        }
        return 0;
    }
    
}
